require("../less/login.less");

document.ready(function() {

    let phone = document.querySelector('#phone');
    let pwd = document.querySelector('#pwd');
    let regBtn = document.querySelector('#regBtn');
    let errMsg = document.querySelector('#errMsg');

    regBtn.addEventListener('click', function() {
        let phoneVal = phone.value;
        let pwdVal = pwd.value;
        errMsg.style.display = 'block';
        if (!phoneVal) {
            errMsg.textContent = '帐号不能为空';
            return;
        }
        if (!pwdVal) {
            errMsg.textContent = '密码不能为空';
            return;
        }
        errMsg.style.display = 'none';
        errMsg.textContent = '';
        //调用注册接口,实现真正的注册(与后端结合)
        axios.post("http://139.9.177.51:8099/users/login", { account: phoneVal, password: pwdVal })
            .then(function(res) {
                if (res.data.status == 0) {
                    window.tipsFn('登录成功', res.data.status);
                    let timeId = setTimeout(function() {
                        location.href = './index.html'
                    }, 1000)
                    localStorage.setItem('userId', res.data.data.user.userId);
                } else {
                    window.tipsFn('用户名/密码错误', res.data.status);
                }
            })
            .catch(function(err) {
                console.log(err);
            })
    })


    //跳转注册界面
    let jumpLogin = document.querySelector('#jumpLogin');
    jumpLogin.addEventListener('click', function() {
        location.href = './register.html';
    })

})